import { Request, RequestHandler, Response } from 'express';
import {
  IGetProjectMembersByIdReq,
  IGetProjectMembersByProjectIdReq,
  IAddProjectMembersReq,
  IDeleteProjectMembersByProjectIdReq
} from '../../models/project-members.model';
import * as ProjectMembersService from '../services/project-members.service';

/**
 * Get active projectMembers records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getProjectMemberList: RequestHandler = async (req: Request, res: Response) => {
  try {
    const projectMemberList = await ProjectMembersService.getProjectMemberList();

    res.status(200).json({
      data: projectMemberList,
      code: 2000,
    })
  } catch (error) {
    console.error('[projectMemberList.controller][getProjectMemberss][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取所有项目成员列表失败！'
    });
  }
};

/**
 * Get projectMembers record based on id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getProjectMembersById: RequestHandler = async (req: IGetProjectMembersByIdReq, res: Response) => {
  try {
    const projectMembers = await ProjectMembersService.getProjectMembersById(req.params.id);

    res.status(200).json({
      data: projectMembers,
      code: 2000,
    })
  } catch (error) {
    console.error('[projectMemberList.controller][getProjectMembersById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取项目成员列表失败！'
    });
  }
};

/**
 * Get projectMembers record based on group id provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getProjectMembersByProjectId: RequestHandler = async (req: IGetProjectMembersByProjectIdReq, res: Response) => {
    try {
      const projectMembers = await ProjectMembersService.getProjectMembersByProjectId(req.params.projectId);

      res.status(200).json({
        data: projectMembers,
        code: 2000,
      })
    } catch (error) {
      console.error('[projectMemberList.controller][getProjectMembersById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
      res.status(500).json({
        message: '获取所有项目成员列表失败！'
      });
    }
  };

/**
 * Inserts a new projectMembers record
 *
 * @param req Express Request
 * @param res Express Response
 */
export const addProjectMembers: RequestHandler = async (req: IAddProjectMembersReq, res: Response) => {
  try {
    const result = await ProjectMembersService.insertProjectMembers(req.body);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[projectMemberList.controller][addProjectMembers][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '添加项目成员失败！'
    });
  }
};

/**
 * deletes a projectMembers
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const deleteProjectMembersByProjectId: RequestHandler = async (req: IDeleteProjectMembersByProjectIdReq, res: Response) => {
  try {
    const result = await ProjectMembersService.deleteProjectMembersByProjectId(req.params.projectId);

    res.status(200).json({
      data: result,
      code: 2000,
    })
  } catch (error) {
    console.error('[projectMemberList.controller][deleteProjectMembersById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '删除项目成员信息失败！'
    });
  }
};